var indexJs = {
    mainSwiper : '',
    mainIndex : 4,
    mainAllow : true,
    mainStart: true,
    INIT () {
        var _this = this;
        //微信分享
        $config.wxShare();
        //自动播放音乐
        (function () {
            var audio = document.getElementById('music');
            if($config.iswx()){
                document.addEventListener("WeixinJSBridgeReady", function () {
                    audio.play();
                }, false);
            }else{
                if(audio.paused){
                    audio.play();
                }
            }

            $('.icon-music').click(function () {
                if($(this).hasClass('ani-music')){
                    $(this).removeClass('ani-music');
                    $(this).addClass('no');
                    audio.pause();
                }else{
                    $(this).addClass('ani-music');
                    $(this).removeClass('no');
                    audio.play();
                }
            })
        }())

        var aniArry = ['ani-into','ani-pulse'];//对应css文件里新增的动画效果
        //全局page
        _this.mainSwiper = new Swiper ('.main-swiper', {
            direction : 'vertical',
            effect : 'fade',
            // speed : 500,
            initialSlide : _this.mainIndex,
            allowTouchMove : false,
            on:{
                init: function(){
                    swiperAnimateCache(this); //隐藏动画元素
                    swiperAnimate(this); //初始化完成开始动画

                    $config.canvas.canvas_dom('Mycanvas1',$(window).height(), 15)

                    //添加提示下滑动画
                    setTimeout(function () {
                        $('.swiper-slide').find('.arrow-down').removeClass('zoomIn').addClass('ani-into');
                    },1500)
                },
                slideChangeTransitionStart (){
                    _this.mainIndex = this.realIndex;
                    console.log(_this.mainIndex)
                    // player.pause();
                    //去除所有新增的动画特效
                    aniArry.forEach(function (item) {
                        $('.'+item).removeClass(item);
                    })
                },
                slideChangeTransitionEnd: function(){
                    swiperAnimate(this); //每个slide切换结束时也运行当前slide动画
                    var index = this.realIndex,
                        type = $('.swiper-slide').eq(index).data('type');

                    //设置背景到这一个slide
                    if(index != 0){
                        $('.swiper-slide').eq(index).append($('#Mycanvas1'));
                    }

                    //设置canvas到这个slide
                    if(index == 1 || index == 2){
                        $('.swiper-slide').eq(index).append($('#c'));
                    }

                    /*if(index == 5){
                        setTimeout(function () {
                            _this.mainSwiper.slideNext();
                        },4000)
                    }*/


                    //添加提示下滑动画
                    setTimeout(function () {
                        $('.swiper-slide').eq(index).find('.arrow-down').removeClass('zoomIn').addClass('ani-into');
                    },1500)
                }
            }
        })

        /*$(".main-swiper").click(function () {
            if(_this.mainAllow){
                //禁止滑动事件
                _this.mainAllow = false;
                //判断当前页面
                if(_this.mainIndex == 0 || _this.mainIndex == 1 || _this.mainIndex == 2){
                    if(_this.mainStart){
                        _this.mainStart = false;
                        _this.animate['slide'+(_this.mainIndex+1)]();
                        $('.swiper-slide').eq(_this.mainIndex).find('.arrow-down').fadeOut(300);
                    }else{
                        let mp3 = document.getElementById('music');
                        mp3.pause();
                        _this.mainSwiper.slideNext()
                        _this.mainAllow = true;
                        _this.mainStart = true;
                    }
                }else if(_this.mainIndex == 3){
                    _this.mainSwiper.slideNext();
                }
            }
        });*/

        //滑动事件
        $('.main-swiper').swipe({
            threshold: 5,
            swipe:function (event, direction) {
                //向上滑，并且可以执行滑动事件
                if(direction == 'up' && _this.mainAllow){
                    //禁止滑动事件
                    _this.mainAllow = false;
                    //判断当前页面
                    if(_this.mainIndex == 0 || _this.mainIndex == 1 || _this.mainIndex == 2){
                        if(_this.mainStart){
                            _this.mainStart = false;
                            _this.animate['slide'+(_this.mainIndex+1)]();
                            $('.swiper-slide').eq(_this.mainIndex).find('.arrow-down').fadeOut(300);
                        }else{
                            /*let mp3 = document.getElementById('music');
                            mp3.pause();*/
                            _this.mainSwiper.slideNext()
                            _this.mainAllow = true;
                            _this.mainStart = true;
                        }
                    }else if(_this.mainIndex == 3){
                        _this.mainSwiper.slideNext();
                    }
                }else {
                    event.preventDefault()
                }
            }
        })

        $('.share-open').click(function () {
            $('.share-box').fadeIn(500);
        });

        $('.share-box').click(function () {
            $('.share-box').fadeOut(300);
        });

        $('.swiper-to').click(function () {
            var to = $(this).data('to');
            if(!to){
                _this.mainSwiper.slideNext();
            }else{
                _this.mainSwiper.slideTo(to)
            }

        })
        
        $('.slide5 .btn').click(function () {
            layer.alert('活动已结束，车展门票快递配送中，请耐心等待！', {closeBtn: 0})
        })
    },
    playComplete (){
        $('.swiper-slide').eq(this.mainIndex).find('.arrow-down').show();
        this.mainAllow = true;
    },
    animate : {
        speed : 2,
        deply : '-=0.4',
        musicPlay(value) {
            return;
            let arrys = {
                '1' : __uri('video/car1.mp3'),
                '2' : __uri('video/car2.mp3'),
                '3' : __uri('video/car3.mp3'),
                '4' : __uri('video/car4.mp3'),
                '5' : __uri('video/car5.mp3'),
            };
            let music = $('#music'),
                mp3 = document.getElementById('music');
            music.attr('src', arrys[value])
            if(value==1 || value==3){
                music.attr('loop', 'loop')
            }else{
                music.removeAttr('loop')
            }
            mp3.play();
        },
        //生成像素点
        pointAni (name, tween){
            let _this = this,
                eleW = $(name+' .pic1').width(),
                eleH = $(name+' .pic1').height(),
                val = 150,
                len = 30,
                i = 1;
            let timer = function () {
                let left = $config.RandomNum(-val,eleW+val),
                    top = $config.RandomNum(-val,eleH+val);
                $(name+' .pic1').append('<i style="left: '+left+'px;top: '+top+'px;"></i>');
                if(i<=len){
                    i++;
                    timer()
                    // setTimeout(timer,2000/len);
                }else{
                    let iW = $(name+' .pic1 i:first').width();
                    tween.staggerTo($(name+' .pic1 i'), _this.speed/3 , {css: {top:eleH/2-iW/2,left:eleW/2-iW/2,"z-index":1}}, 0.01);
                }
            }
            timer();
        },
        slide1 (){
            let name = '.slide1';
            $('.slide1 .pic1').removeClass('zoomIn');
            let _this = this,
                tween = new TimelineMax();
            _this.pointAni(name, tween);
            tween.to(name+' .pic1', _this.speed, {autoAlpha : 0})
            tween.to(name+' .pic2', _this.speed, {autoAlpha : 1,
                onStart(){
                    _this.musicPlay(1);
                }
            }, '-='+_this.speed)
            tween.to(name+' .pic2', _this.speed, {rotation : 60}, '-='+_this.speed/2)
            tween.to(name+' .pic2', _this.speed, {scale : (.3,.3),autoAlpha: 0,
                onStart(){
                    $config.canvas.canvasScene.init();
                    setTimeout(function () {
                        $config.canvas.canvasScene.dataType(0)
                    },2000)
                }
            })
            tween.fromTo(name+' .pic3', _this.speed, {scale : (.3,.3), autoAlpha : 0}, {scale : (1,1), autoAlpha : 1,
                onStart(){
                    _this.musicPlay(2);
                },
                onComplete(){
                    $('.slide1 .pic3').addClass('ani-breathe');
                    indexJs.playComplete();
                }
            })
            return tween;
        },
        slide2 (){
            let name = '.slide2';
            $('.slide2 .pic1').removeClass('zoomIn');
            let _this = this,
                tween = new TimelineMax();
            _this.pointAni(name, tween);
            tween.to(name+' .pic1', _this.speed, {autoAlpha : 0})
            tween.to(name+' .pic2', _this.speed, {autoAlpha : 1,
                onStart(){
                    _this.musicPlay(3);
                }
            }, '-='+_this.speed)
            tween.to(name+' .pic2', _this.speed/3, {scale : (.5,.5),ease: Power2.easeIn})
            tween.to(name+' .pic2', _this.speed/3, {scale : (1,1),ease: Power2.easeOut})

            tween.to(name+' .pic2', _this.speed, {scale : (.1,.1),autoAlpha: 0,
                onStart(){
                    $config.canvas.canvasScene.dataType(1)
                    setTimeout(function () {$config.canvas.canvasScene.dataType(2)},1000)
                    setTimeout(function () {$config.canvas.canvasScene.dataType(0)},2000)
                }
            })

            tween.fromTo(name+' .pic3', _this.speed, {scale : (.3,.3), autoAlpha : 0,},{scale : (1,1), autoAlpha : 1,
                onComplete(){
                    $(name+' .pic3').addClass('ani-pulse');
                    indexJs.playComplete();
                }
            })
            return tween;
        },
        slide3 (){
            let name = '.slide3';
            $(name+' .pic1').removeClass('zoomIn');
            let _this = this,
                tween = new TimelineMax();
            _this.pointAni(name, tween);
            tween.to(name+' .pic1', _this.speed, {autoAlpha : 0})
            tween.to(name+' .pic2', _this.speed, {autoAlpha : 1,
                onStart(){
                    setTimeout(function () {
                        _this.musicPlay(4);
                    },800)
                }
            }, '-='+_this.speed)
            tween.to(name+' .pic2', _this.speed, {scale : (1.2,1.2)})
            tween.to(name+' .pic2', _this.speed/2, {scale : (.1,.1),autoAlpha: 0,
                onStart(){
                    $config.canvas.canvasScene.dataType(1)
                    setTimeout(function () {$config.canvas.canvasScene.dataType(2)},1000)
                    setTimeout(function () {$config.canvas.canvasScene.dataType(0)},2000)
                }
            })
            tween.fromTo(name+' .pic3', _this.speed, {scale : (.3,.3), autoAlpha : 0}, {scale : (1,1), autoAlpha : 1,
                onStart(){
                    _this.musicPlay(5);
                },
                onComplete(){
                    indexJs.playComplete();
                }
            }, '+='+_this.speed/2)
            return tween;
        },
    }
}

//angular初始化
var App = angular.module('App', []);
    App.controller('forms',['$scope', function(scope) {
        //表单数据
        scope.infos = {
            name : '',
            mobile : '',
            address : ''
        }
        //提交数据
        scope.submitForm = ()=>{
            var Msg = '';
            // 表单校验
            if (!scope.infos.name) {
                Msg = '请输入您的姓名！';
            } else if (!scope.infos.mobile) {
                Msg = '请输入您的手机号！';
            }else if (!$config.rex.mobile.test(scope.infos.mobile)) {
                Msg = '请输入正确的手机号！';
            }else if (!scope.infos.address) {
                Msg = '请输入邮寄地址！';
            }
            if (Msg) {
                layer.alert(Msg, {closeBtn: 0})
                return
            };

            var layerBox =  layer.load(2);

            $.ajax({
                url: $config.host()+"/Autoshow/SaveUserInfo",
                dataType: "text",
                data: {
                    username: scope.infos.name,
                    phoneNbr: scope.infos.mobile,
                    address: scope.infos.address
                },
                success: function (json) {
                    if(typeof json == 'string'){
                        json = JSON.parse(json)
                    }
                    if(json.success){
                        indexJs.mainSwiper.slideNext();
                    }else{
                        layer.alert(json.msg, {closeBtn: 0})
                    }
                    layer.close(layerBox);
                }
            });

        }
    }]);

window.onload = function () {
    document.body.style.display = 'block';
    //初始化swiper
    indexJs.INIT();
}



